package com.mybook.app.controller;

import com.mybook.app.core.BaseResponse;
import com.mybook.app.error.Error;
import com.mybook.app.model.UserInfo;
import com.mybook.app.model.UserInfoToken;
import com.mybook.app.request.*;
import com.mybook.app.response.SmsInfo;
import com.mybook.app.response.Token;
import com.mybook.app.response.UserInfoDetail;
import com.mybook.app.service.UserService;
import com.mybook.app.utils.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Created by zjutsw on 2017/10/27.
 * <p>
 * 短信发送和验证码校验
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping("/sms")
    public BaseResponse sendSms(@RequestBody @Valid SmsRequest smsRequest) {
        Logger.debug(this, "获取验证码： " + smsRequest.getMobile());
        String uuid = userService.sendSms(smsRequest);
        return new BaseResponse(uuid);
    }

    @PostMapping("/sms/check")
    public BaseResponse checkSms(@RequestBody @Valid SmsCheckRequest smsRequest) {
        Logger.debug(this, "验证验证码： " + smsRequest);
        SmsInfo smsInfo = userService.checkSms(smsRequest);
        if (null == smsInfo) {
            return BaseResponse.ok();
        }
        return new BaseResponse(Error.VAILD_SMS_CODE, smsInfo);
    }

    @PostMapping("/register")
    public BaseResponse register(@RequestBody @Valid UserRegisterRequest userRegisterRequest) {
        Logger.debug(this, "用户注册： " + userRegisterRequest);
        userService.register(userRegisterRequest);
        return BaseResponse.ok();
    }

    @PostMapping("/login")
    public BaseResponse register(@RequestBody @Valid UserLoginRequest userRegisterRequest) {
        Logger.debug(this, "用户登录： " + userRegisterRequest);
        Token token = userService.login(userRegisterRequest);
        return new BaseResponse(token);
    }

    @PostMapping("/info")
    public BaseResponse userInfo() {
        Logger.debug(this, "获取用户信息： ");
        UserInfoDetail userInfoDetail = userService.userInfo();
        return new BaseResponse(userInfoDetail);
    }

    @PostMapping("/coor")
    public BaseResponse coor(@RequestBody CoorRequest coorRequest) {
        Logger.debug(this, "获取用户信息： ");
        userService.coor(coorRequest.getCoor());
        return BaseResponse.ok();
    }

}
